/**
 * Copyright (C) 2015-2018 IQIYI
 * All rights reserved.
 *
 * Author     : 王海斌
 * E-mail     : wanghaibin@qiyi.com
 * Version    :
 * Date       : Tue 20 Nov 2018 10:57:24 AM CST
 * Destription: 测试函数指针指向类的纯虚函数,
 *
 * 结论：
 *      当函数指针指向类的纯虚函数时, 在实际调用时调用的是子类的实现函数
 *
 */

#include <iostream>
#include <stdio.h>
#include <string>
#include <map>

using namespace std;

class TeseBase
{
public:
    TeseBase (){
        handles_["func1"] = &TeseBase::func1;
        handles_["func2"] = &TeseBase::func2;
    }
    virtual ~TeseBase (){
    }

    void parse(const char * type, int data) {
        std::map<std::string, TestHandler>::iterator it =  handles_.find(type);
        if (it != handles_.end()) {
            (this->*it->second)(data);
        } else {
            printf("not find type=%s\n", type);
        }
    }

protected:
    virtual void func1(int data) = 0;
    virtual void func2(int data) = 0;

private:
    typedef void (TeseBase::*TestHandler)(int);
    std::map<std::string, TestHandler> handles_;
};

class TestChildA : public TeseBase {
public:
protected:
    virtual void func1(int data)  { printf(" current is TestChildA::func1, data=%d\n", data);};
    virtual void func2(int data)  { printf(" current is TestChildA::func2, data=%d\n", data);};
};


class TestChildB : public TeseBase {
public:
protected:
    virtual void func1(int data)  { printf(" current is TestChildB::func1, data=%d\n", data);};
    virtual void func2(int data)  { printf(" current is TestChildB::func2, data=%d\n", data);};
};

int main(int argc, char *argv[])
{

    TeseBase * t1 = new TestChildA;
    TeseBase * t2 = new TestChildB;
    t1->parse("func1", 100);
    t2->parse("func1", 100);

    delete t1;
    delete t2;

    return 0;
}
